﻿@model YiYiTong.Rocket.Model.Manager.RoleModel

@{
    ViewBag.Title = "角色设置菜单";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<form method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td colspan="2">
                <h5>角色设置菜单项，标<em>*</em>号是必填或必选项。</h5>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">角色编号</td>
            <td><input type="text" id="RoleID" name="RoleID" readonly="readonly" value="@Model.roleId" /></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">角色名称</td>
            <td><input type="text" name="RoleName" readonly="readonly" value="@Model.roleName" /></td>
        </tr>
        <tr>
            <td class="tableleft">菜单项<em>*</em></td>
            <td>
                <div id="dvMenu"></div>
                <input id="hidmenuIds" type="hidden" value="@Model.MenuIds" />
            </td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <p><span id="spMsg"></span></p>
                <button type="button" class="btn btn-primary" id="save">保存</button> &nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回</button>
            </td>
        </tr>
    </table>
    <script>
        BUI.use(['bui/overlay', 'bui/tree'], function (overlay, Tree) {
            function showSucc(obj, url) {
                BUI.Message.Alert(obj, function () {
                    location.href = url;
                }, 'success');
            }

            //树节点数据，
            //text : 文本，
            //id : 节点的id,
            //leaf ：标示是否叶子节点，可以不提供，根据childern,是否为空判断
            //expanded ： 是否默认展开
            //checked : 节点是否默认选中
            var roleid = '@Model.roleId';
            $.post('@Url.Action("GetChkBoxMenuList", "Menu")', { roleId: roleid }, function (result) {
                var data = JSON.parse(result);
                //由于这个树，不显示根节点，所以可以不指定根节点
                var tree = new Tree.TreeList({
                    render: '#dvMenu',
                    nodes: data,
                    checkType: 'all', //checkType:勾选模式，提供了4中，all,onlyLeaf,none,custom
                    showLine: true //显示连接线
                });
                tree.render();

                tree.on('itemclick', function (ev) {
                    var checkedNodes = tree.getCheckedNodes();
                    var str = '';
                    BUI.each(checkedNodes, function (node) {
                        str += node.id + ',';
                    });
                    $('#hidmenuIds').val(str);
                });
            });

            //返回列表
            $('#backid').on('click', function () {
                window.document.location.href = "@Url.Action("RoleList", "Role")";
            });

            //保存
            $('#save').on('click', function () {
                var roleid = '@Model.roleId';
                var menuIds = $("#hidmenuIds").val();
                if (menuIds == "") {
                    $("#spMsg").html('请选择菜单项');
                    return;
                }
                //post提交
                $.post('@Url.Action("SetMenu","Role")', { RoleId: roleid, MenuIds: menuIds }, function (result) {
                    if (result.Message != undefined) {
                        if (result.IsSuccess) {
                            showSucc(result.Message, result.Body);
                        }
                        else {
                            $("#spMsg").html(result.Message);
                        }
                    }
                    else {
                        var substr = cutstr(result, "BUI.Message.Alert", "function");
                        substr = substr.replace("'", "").replace("'", "").replace("(", "").replace(",", "");
                        if (substr == "")
                            BUI.Message.Alert("操作失败，请联系技术人员", 'warning');
                        else {
                            if (substr.indexOf("请重新登录") > 0) {
                                BUI.Message.Alert(substr, function () {
                                    top.location.href = "@Url.Action("Login","User")";
                                }, 'warning');
                            }
                            else {
                                BUI.Message.Alert(substr, 'warning');
                            }
                        }
                    }
                });
            });
        });
    </script>
</form>
